package apis

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"go-admin/app/admin/models"
	"go-admin/common/auth/member"

	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
)

type SysMemberType struct {
	api.Api
}

// GetPage
// @Summary 分页会员类型列表数据
// @Description 分页列表
// @Tags 会员类型
// @Param typeName query string false "typeName"
// @Param typeId query string false "typeId"
// @Param position query string false "position"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys-member-type [get]
// @Security Bearer
func (e SysMemberType) GetPage(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	list := make([]models.SysMemberType, 0)
	list, err = s.SetMemberTypePage(&req)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}
	e.OK(list, "查询成功")
}

// Get
// @Summary 获取会员类型数据
// @Description 获取JSON
// @Tags 会员类型
// @Param typeId path string false "typeId"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys-member-type/{deptId} [get]
// @Security Bearer
func (e SysMemberType) Get(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.SysMemberType

	err = s.Get(&req, &object)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}

	e.OK(object, "查询成功")
}

// Insert 添加会员类型
// @Summary 添加会员类型
// @Description 获取JSON
// @Tags 会员类型
// @Accept  application/json
// @Product application/json
// @Param data body dto.SysMemberTypeInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/sys-member-type [post]
// @Security Bearer
func (e SysMemberType) Insert(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// 设置创建人
	req.SetCreateBy(member.GetMemberId(c))
	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, "创建失败")
		return
	}
	e.OK(req.GetId(), "创建成功")
}

// Update
// @Summary 修改会员类型
// @Description 获取JSON
// @Tags 会员类型
// @Accept  application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.SysMemberTypeUpdateReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/sys-member-type/{typeId} [put]
// @Security Bearer
func (e SysMemberType) Update(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(member.GetMemberId(c))
	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.GetId(), "更新成功")
}

// Delete
// @Summary 删除会员类型
// @Description 删除数据
// @Tags 会员类型
// @Param data body dto.SysMemberTypeDeleteReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/v1/sys-member-type [delete]
// @Security Bearer
func (e SysMemberType) Delete(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(500, err, "删除失败")
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// Get2Tree 会员类型管理 左侧部门树
func (e SysMemberType) Get2Tree(c *gin.Context) {
	s := service.SysMemberType{}
	req := dto.SysMemberTypeGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	list := make([]dto.TypeLabel, 0)
	list, err = s.SetMemberTypeTree(&req)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}
	e.OK(list, "")
}
